package com.tn.openrpc.channel.secure;

import org.springframework.security.core.context.SecurityContextHolder;

import com.tn.openrpc.util.FactoryException;
import com.tn.openrpc.util.ParametrizedFactory;

/**
 * An implementation of <code>ParametrizedFactory</code> that creates a <code>SecureRemoteChannelRequest</code> obtain the
 * <code>Authentication</code> from a call to <code>SecurityContextHolder.getContext().getAuthentication()</code>.
 */
public class SecureRemoteChannelRequestFactory implements ParametrizedFactory<SecureRemoteChannelRequest, Object[]>
{
  private String target;
  private String methodName;

  /**
   * Creates a new <code>SecureRemoteChannelRequestFactory</code> initialized with the <code>target</code> and <code>methodName</code>.
   */
  public SecureRemoteChannelRequestFactory(String target, String methodName)
  {
    this.target = target;
    this.methodName = methodName;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public SecureRemoteChannelRequest newInstance(Object[] args) throws FactoryException
  {
    return new SecureRemoteChannelRequest(
      SecurityContextHolder.getContext().getAuthentication(),
      target,
      methodName,
      args
    );
  }
}
